package com.wl.wuliu.controller;

import com.wl.wuliu.core.controller.BaseController;
import com.wl.wuliu.core.page.TableDataInfo;
import com.wl.wuliu.entity.Driver;
import com.wl.wuliu.entity.SysUser;
import com.wl.wuliu.result.AjaxResult;
import com.wl.wuliu.service.DriverService;
import com.wl.wuliu.service.SysUserService;
import com.wl.wuliu.utils.PatternUtils;
import com.wl.wuliu.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: gutaozhen
 * @Date: 2024/5/15
 */
@RestController
@RequestMapping("/driver")
public class DriverController extends BaseController {

    @Autowired
    private DriverService driverService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表查询
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo findList(Driver driver) {
        //如果不是联络员，查询全部
        if (!StringUtils.isEmpty(driver.getUserId())) {
            SysUser sysUser = sysUserService.findSysUser(new SysUser(driver.getUserId()));
            if (sysUser != null && !"lianluo".equals(sysUser.getRoleKey())) {
                driver.setUserId(null);
            }
        }
        startPage();
        List<Driver> list = driverService.findList(driver);

        return getDataTable(list);
    }

    /**
     * 保存司机信息
     * @param driver
     * @return
     */
    @PostMapping("/save")
    public AjaxResult saveDriver(@RequestBody Driver driver) {

        if (StringUtils.isEmpty(driver.getCarNumber())) {
            return AjaxResult.error("车牌号不能为空。");
        }
        if (StringUtils.isEmpty(driver.getDriverName())) {
            return AjaxResult.error("司机姓名不能为空。");
        }
        if (StringUtils.isEmpty(driver.getDriverMobile())) {
            return AjaxResult.error("司机手机号码不能为空。");
        }else {
            if (!PatternUtils.validatePhoneNumber(driver.getDriverMobile())) {
                return AjaxResult.error("手机号不规范。");
            }
        }
        if (StringUtils.isEmpty(driver.getUserId())) {
            return AjaxResult.error("联络员不能为空。");
        }

        return driverService.saveDriver(driver);
    }

    /**
     * 删除司机信息
     * @return
     */
    @GetMapping("/delete")
    public AjaxResult deleteDriver(@RequestParam String id) {

        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("非法参数");
        }

        return driverService.deleteDriver(id);
    }

    /**
     * 修改司机信息
     * @param driver
     * @return
     */
    @PostMapping("/update")
    public AjaxResult updateDriver(@RequestBody Driver driver) {

        return driverService.updateDriver(driver);
    }

    /**
     * 根据id查询司机信息
     * @param id
     * @return
     */
    @GetMapping("/getDriver")
    public AjaxResult getDriver(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("非法参数");
        }

        Driver driver = driverService.getDriver(id);

        return AjaxResult.success(driver);
    }


}
